/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ses;

import Entity.AdvertiseType;
import ses.exceptions.IllegalOrphanException;
import ses.exceptions.NonexistentEntityException;
import ses.exceptions.RollbackFailureException;
import java.io.Serializable;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import Entity.Advertise;
import java.util.ArrayList;
import java.util.List;
import javax.transaction.UserTransaction;

/**
 *
 * @author Huu Vinh
 */
public class AdvertiseTypeJpaController implements Serializable {

    public AdvertiseTypeJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(AdvertiseType advertiseType) throws RollbackFailureException, Exception {
        if (advertiseType.getAdvertiseList() == null) {
            advertiseType.setAdvertiseList(new ArrayList<Advertise>());
        }
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            List<Advertise> attachedAdvertiseList = new ArrayList<Advertise>();
            for (Advertise advertiseListAdvertiseToAttach : advertiseType.getAdvertiseList()) {
                advertiseListAdvertiseToAttach = em.getReference(advertiseListAdvertiseToAttach.getClass(), advertiseListAdvertiseToAttach.getAdvertiseId());
                attachedAdvertiseList.add(advertiseListAdvertiseToAttach);
            }
            advertiseType.setAdvertiseList(attachedAdvertiseList);
            em.persist(advertiseType);
            for (Advertise advertiseListAdvertise : advertiseType.getAdvertiseList()) {
                AdvertiseType oldAdvertiseTypeIdOfAdvertiseListAdvertise = advertiseListAdvertise.getAdvertiseTypeId();
                advertiseListAdvertise.setAdvertiseTypeId(advertiseType);
                advertiseListAdvertise = em.merge(advertiseListAdvertise);
                if (oldAdvertiseTypeIdOfAdvertiseListAdvertise != null) {
                    oldAdvertiseTypeIdOfAdvertiseListAdvertise.getAdvertiseList().remove(advertiseListAdvertise);
                    oldAdvertiseTypeIdOfAdvertiseListAdvertise = em.merge(oldAdvertiseTypeIdOfAdvertiseListAdvertise);
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(AdvertiseType advertiseType) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            AdvertiseType persistentAdvertiseType = em.find(AdvertiseType.class, advertiseType.getAdvertiseTypeId());
            List<Advertise> advertiseListOld = persistentAdvertiseType.getAdvertiseList();
            List<Advertise> advertiseListNew = advertiseType.getAdvertiseList();
            List<String> illegalOrphanMessages = null;
            for (Advertise advertiseListOldAdvertise : advertiseListOld) {
                if (!advertiseListNew.contains(advertiseListOldAdvertise)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Advertise " + advertiseListOldAdvertise + " since its advertiseTypeId field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<Advertise> attachedAdvertiseListNew = new ArrayList<Advertise>();
            for (Advertise advertiseListNewAdvertiseToAttach : advertiseListNew) {
                advertiseListNewAdvertiseToAttach = em.getReference(advertiseListNewAdvertiseToAttach.getClass(), advertiseListNewAdvertiseToAttach.getAdvertiseId());
                attachedAdvertiseListNew.add(advertiseListNewAdvertiseToAttach);
            }
            advertiseListNew = attachedAdvertiseListNew;
            advertiseType.setAdvertiseList(advertiseListNew);
            advertiseType = em.merge(advertiseType);
            for (Advertise advertiseListNewAdvertise : advertiseListNew) {
                if (!advertiseListOld.contains(advertiseListNewAdvertise)) {
                    AdvertiseType oldAdvertiseTypeIdOfAdvertiseListNewAdvertise = advertiseListNewAdvertise.getAdvertiseTypeId();
                    advertiseListNewAdvertise.setAdvertiseTypeId(advertiseType);
                    advertiseListNewAdvertise = em.merge(advertiseListNewAdvertise);
                    if (oldAdvertiseTypeIdOfAdvertiseListNewAdvertise != null && !oldAdvertiseTypeIdOfAdvertiseListNewAdvertise.equals(advertiseType)) {
                        oldAdvertiseTypeIdOfAdvertiseListNewAdvertise.getAdvertiseList().remove(advertiseListNewAdvertise);
                        oldAdvertiseTypeIdOfAdvertiseListNewAdvertise = em.merge(oldAdvertiseTypeIdOfAdvertiseListNewAdvertise);
                    }
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = advertiseType.getAdvertiseTypeId();
                if (findAdvertiseType(id) == null) {
                    throw new NonexistentEntityException("The advertiseType with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            AdvertiseType advertiseType;
            try {
                advertiseType = em.getReference(AdvertiseType.class, id);
                advertiseType.getAdvertiseTypeId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The advertiseType with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<Advertise> advertiseListOrphanCheck = advertiseType.getAdvertiseList();
            for (Advertise advertiseListOrphanCheckAdvertise : advertiseListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This AdvertiseType (" + advertiseType + ") cannot be destroyed since the Advertise " + advertiseListOrphanCheckAdvertise + " in its advertiseList field has a non-nullable advertiseTypeId field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(advertiseType);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<AdvertiseType> findAdvertiseTypeEntities() {
        return findAdvertiseTypeEntities(true, -1, -1);
    }

    public List<AdvertiseType> findAdvertiseTypeEntities(int maxResults, int firstResult) {
        return findAdvertiseTypeEntities(false, maxResults, firstResult);
    }

    private List<AdvertiseType> findAdvertiseTypeEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from AdvertiseType as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public AdvertiseType findAdvertiseType(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(AdvertiseType.class, id);
        } finally {
            em.close();
        }
    }

    public int getAdvertiseTypeCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from AdvertiseType as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
